// pages/start/start.js
const app = getApp()
const userDB = wx.cloud.database().collection("user")
const recordsDB = wx.cloud.database().collection("records")

Page({
  data: {
    imgs:[
      "cloud://xiaotiao-6gcr9hxy3d8aa90f.7869-xiaotiao-6gcr9hxy3d8aa90f-1309378747/s1.png",
      "cloud://xiaotiao-6gcr9hxy3d8aa90f.7869-xiaotiao-6gcr9hxy3d8aa90f-1309378747/s2.png",
      "cloud://xiaotiao-6gcr9hxy3d8aa90f.7869-xiaotiao-6gcr9hxy3d8aa90f-1309378747/s3.png",
    ],
  },

  start(){
    let flag=false; //账号默认不存在
    let recordsflag=false;
    if(app.globalData.userInfo==null){
      wx.getUserProfile({
        desc: '获取用户必要的信息',
        success: (res) => {
          this.setData({ show: false }),
          app.globalData.userInfo=res.userInfo
          console.log(app.globalData.userInfo)
          userDB.get({
            success: (res) => {
              console.log("数据库里user表的数据",res.data)
              let user=res.data;
              for (let i = 0; i < user.length; i++) {  //遍历数据库对象集合
                if (app.globalData.openid === user[i]._openid) { //账户已存在
                  flag=true
                  break
                }
              }
              console.log("看一下我的flag",flag)
              if(flag==false){
                userDB.add({
                  data:{
                    nickName:app.globalData.userInfo.nickName,
                    avatarUrl:app.globalData.userInfo.avatarUrl,
                    friend:"暂无",
                    dakaDays:0,
                    //记得到时候要改成0
                    friendTime:0,
                    level:"待测试",
                  }
                })
                flag=true
              }
            }
          })

          recordsDB.get({
            success: (res) => {
              console.log(res.data)
              let records=res.data;
              for (let i = 0; i < records.length; i++) {  //遍历数据库对象集合
                if (app.globalData.openid === records[i]._openid) { //账户已存在
                  recordsflag=true
                  break
                }
              }
              if(recordsflag==false){
                recordsDB.add({
                  data:{
                    alcohol:0,
                    diet:0,
                    encourage:0,
                    remind:0,
                    emotion:0,
                    sleep:0,
                    sport:0,
                    totalday:0,
                  }
                })
                recordsflag=true
              }
            }
          })

        }
      })
    }
    wx.switchTab({
      url: '../index/index',
    })
  },

})